#include "KEY.h"
#include <ti/driverlib/dl_gpio.h>
#include "delay.h"


void Key_Init(void)
{	
	DL_GPIO_initDigitalInputFeatures(GPIO_KEYS_PA14_KEY_S2_IOMUX,
		 DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_PULL_UP,
		 DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
	
	DL_GPIO_initDigitalInputFeatures(GPIO_KEYS_PA18_KEY_S1_IOMUX,
		 DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_NONE,
	   DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);        
}

uint8_t Key_Scan(uint8_t mode)
{
	static uint8_t key_up = 1; 
	static uint8_t sw1_sta=0;
	static uint8_t sw2_sta=1;
	uint8_t keyval = 0;
	
	if(DL_GPIO_readPins(GPIOA, GPIO_KEYS_PA18_KEY_S1_PIN))
	{
		sw1_sta=1;
	}
	else
	{
		sw1_sta=0;
	}
	
	if(!DL_GPIO_readPins(GPIOA, GPIO_KEYS_PA14_KEY_S2_PIN))
	{
		sw2_sta=1;
	}
	else
	{
		sw2_sta=0;
	}
	
	if (mode) key_up = 1;     

	if (key_up && ((sw2_sta == 1)||(sw1_sta == 1)))  
	{
			delay_cycles(32000*10);		
			key_up = 0;
			
			if (sw1_sta == 1) keyval = SW1_PRES;
			if (sw2_sta == 1) keyval = SW2_PRES;
	}
	else if ((sw1_sta==0) && (sw2_sta==0))       
	{
			key_up = 1;
	}

	return keyval;             
}


